//
//  CZNetworkTool.swift
//  iOSWeibo10
//
//  Created by 李伟鹏 on 16/5/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestMethod: String {
    case GET = "GET"
    case POST = "POST"
}

class CZNetworkTool: NSObject {
    
    static let sharedInstance: CZNetworkTool = CZNetworkTool()
    private let afnManager: AFHTTPSessionManager = {
       let afn = AFHTTPSessionManager(baseURL: NSURL(string: "https://api.weibo.com/"))
        afn.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return afn
    }()
    
    func request(method: RequestMethod, URLString: String, parameters: AnyObject?,success:((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?){
        if method == RequestMethod.GET{
            afnManager.GET(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }else{
            afnManager.POST(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
    }
    
    func POST(URLString: String, parameters: AnyObject?, constructingBodyWithBlock: ((AFMultipartFormData) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {
        afnManager.POST(URLString, parameters: parameters, constructingBodyWithBlock: constructingBodyWithBlock, progress: nil, success: success, failure: failure)
    }
}
